#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "cJSON/cJSON.h"
#include "cgic.h"

#define MSG_NAME "/msg_file_vdec"


typedef struct
{
    long mtype;
    int data;
} Message;

Message message;

int cgiMain()
// int main()
{

    key_t key_msg;
	int msgid;

	key_msg = ftok(MSG_NAME, 'k');

	msgid = msgget(key_msg, 0666 | O_RDONLY);
	printf("msgid = %d\n", msgid);

    char url_address[100];
    char record_file[100];

    FILE *file = fopen("/demo/bin/boa/www/cjson_dir/network_detail.json", "r");
    if (file == NULL)
    {
        perror("打开文件时出错");
        return EXIT_FAILURE;
    }
    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);
    char *data = (char *)malloc(length + 1);
    fread(data, 1, length, file);
    data[length] = '\0';
    cJSON *json = cJSON_Parse(data);
    if (json == NULL)
    {
        printf("json parse error....\n");
    }
    
    cgiHeaderContentType("text/html");
    fprintf(cgiOut, "<HTML>\n");
    fprintf(cgiOut, "<TITLE>Picture Manage</TITLE>\n");
    fprintf(cgiOut, "<HEAD>\n");
    fprintf(cgiOut, "<link rel='stylesheet' href='../common.css'>\n");
    fprintf(cgiOut, "</HEAD>\n");
    fprintf(cgiOut, "<BODY>\n");
    fprintf(cgiOut, "<ul class='menu'>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_dir_cgi.cgi'>Photo_Manage</a></li>\n");
    fprintf(cgiOut, "<li><a href='../ffmpeg_config.html'>Edit_URL</a></li>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_dir_cgi.cgi'>URL_Manage</a></li>\n");
    fprintf(cgiOut, "<li><a href='../decode_video.html'>Remote_Moniting</a></li>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_video_cgi.cgi'>Video_Manage</a></li>\n");
    fprintf(cgiOut, "</ul>\n");
    cgiFormString("url_address", url_address, 100);
    if (url_address != NULL)
    {
        fprintf(cgiOut, "<H1>%s</H1>", url_address);
        cJSON_ReplaceItemInObject(json, "url_address", cJSON_CreateString(url_address));
    }
    else
    {
        fprintf(cgiOut, "<H1>NULL URL_ADDRESS</H1>");
    }

    /*cgiFormString("record_file", record_file, 100);
    if (record_file != NULL)
    {
        fprintf(cgiOut, "<H1>%s</H1>", record_file);
        cJSON_ReplaceItemInObject(json, "record_file", cJSON_CreateString(record_file));
    }
    else
    {
        fprintf(cgiOut, "<H1>NULL MP4_NAME</H1>");
    }*/

    fprintf(cgiOut, "<a href=\"/cgi-bin/index_button.cgi\">To Main Page</a>\n");
    fprintf(cgiOut, "</BODY>\n");
    fprintf(cgiOut, "</HTML>\n");

    char *new_json_str = cJSON_PrintUnformatted(json);
    // 将新的JSON字符串写入文件
    FILE *new_file = fopen("/www/cjson_dir/network_detail.json", "w");
    if (new_file == NULL)
    {
        printf("Can not Create New File....\n");
        return -1;
    }
    fputs(new_json_str, new_file);

    message.mtype = 1;
    message.data = 4;

    if (msgsnd(msgid, &message, sizeof(int), 0) == -1) //发送消息队列指令
    {
        fprintf(cgiOut, "<H2>ERROR SEND QUEUE</H2>");
        //exit(1);
    }
    else
    {
        fprintf(cgiOut, "<H2>SUCCESS SEND QUEUE</H2>");
    }

    // fprintf(cgiOut, "<script type=text/javascript>window.location= 'index_button.cgi'></script>");
    return 0;
}
